//
//  BabyBLEViewController.m
//  BlueToothDemo
//
//  Created by US bike on 2017/8/14.
//  Copyright © 2017年 US bike. All rights reserved.
//

#import "BabyBLEViewController.h"
#import "BlueToothManager.h"
#import "Tools.h"


@interface BabyBLEViewController ()
@property (weak, nonatomic) IBOutlet UILabel *notifyReceiveLbl;
@property (weak, nonatomic) IBOutlet UILabel *readReceiveLbl;
@property (weak, nonatomic) IBOutlet UITextField *senderTextField;
@property (weak, nonatomic) IBOutlet UITextView *logTextView;


@end

@implementation BabyBLEViewController{
    NSInteger index;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"创建界面");
    
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenAction)]];
    
    [[BlueToothManager sharedBlueToothManager] setupBeforeConnectWithName:@"iPhone"];
    
    __weak typeof(self) weakSelf = self;
    [[BlueToothManager sharedBlueToothManager] setupBlueToothFailureBlock:^{
        [Tools debugLog:@"蓝牙设备连接失败" AndTextView:weakSelf.logTextView];
        NSLog(@"蓝牙设备连接失败");

    }];
    
    [[BlueToothManager sharedBlueToothManager] setupReceiveMessageBlock:^(Boolean succed, BlueToothResponse *response) {
        if (succed) {
            
            
            
            
            NSString *logStr = [NSString stringWithFormat:@"%@：%@",response.info, [[NSString alloc] initWithData:response.data encoding:NSUTF8StringEncoding]];
            [Tools debugLog:logStr AndTextView:weakSelf.logTextView];
            
            NSLog(@"%@",logStr);
            
            
            
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
            
            NSURLSession *session = [NSURLSession sharedSession];
            [[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                
                NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                
                NSLog(@"网络请求返回：%@",str);
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    index++;

                    NSString *sendStr = [NSString stringWithFormat:@"收到了，别吵啦 %ld",index];
                    [[BlueToothManager sharedBlueToothManager] sendMessageData:[sendStr dataUsingEncoding:NSUTF8StringEncoding]];
                    NSString *logStr = [NSString stringWithFormat:@"发送的数据为：%@",sendStr];
                    [Tools debugLog:logStr AndTextView:self.logTextView];
                });

                

                
            }] resume];
            

        } else {
            NSString *logStr = [NSString stringWithFormat:@"%@：%@",response.info, response.error];

            [Tools debugLog:logStr AndTextView:weakSelf.logTextView];
            NSLog(@"%@",logStr);


        }
    }];
    
}



- (IBAction)sendAction:(id)sender {

    if ([self.senderTextField hasText]) {
        
        [[BlueToothManager sharedBlueToothManager] sendMessageData:[self.senderTextField.text dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *logStr = [NSString stringWithFormat:@"发送的数据为：%@",self.senderTextField.text];
        [Tools debugLog:logStr AndTextView:self.logTextView];  
        

    } else {
        [Tools debugLog:@"发送的数据为空" AndTextView:self.logTextView];
        NSLog(@"发送的数据为空");
    }
}


- (void) hiddenAction {
    [self.view endEditing:YES];
}

- (void) dealloc {
    
    [[BlueToothManager sharedBlueToothManager] cancellAll];
    
    NSLog(@"界面销毁");
}
@end
